layui.define(['table', 'form', 'element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, element = layui.element;

  var userId = $('input[name="userId"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/agent/busi_config/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'businessType', title: '业务类型'}
            ,{field: 'businessTypeName', title: '业务类型名称'}
            ,{field: 'serviceChargeRatio', title: '服务费比例',templet: function (record){
                    return record.serviceChargeRatio + '%';
                }}
            ,{field: 'createDate', title: '创建时间'}
			,{title: '操作', width: 120, align:'center', fixed: 'right',templet:function (record){
				var html = '<a class="layui-btn layui-btn-xs" lay-event="updateBusiConfig">修改</a>';
                html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delBusiConfig">删除</a>';
                return html;
            }}
        ]]
        ,page: true
        ,limit: 30
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

	table.render({
        elem: '#LAY-table-type'
        ,url: layui.setter.webRoot + '/agent/type_rel/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'typeId', title: '类型ID'}
            ,{field: 'typeName', title: '类型名称'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 80, align:'center', fixed: 'right',templet:function (record){
                var html = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delType">移除</a>';
                return html;
            }}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });
	
	table.render({
        elem: '#LAY-table-func-model'
        ,url: layui.setter.webRoot + '/agent/func_model/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,size:'lg'
        ,cols: [[
            {field: 'funcModelId', title: 'ID'}
            ,{field: 'name', title: '功能模块名称',templet:function (record){
                return record.funcModel.name;
             }}
            ,{field: 'type', title: '功能类型',templet:function (record){
                    return record.funcModel.typeName;
                }}
            ,{field: 'iconUrl', title: 'ICON图标',templet:function (record){
                    if(record.funcModel.iconUrl) {
                        return '<img src="' + record.funcModel.iconUrl + '" style="max-height:100%;margin:auto;display:block;">';
                    }else{
                        return '<img src="'+layui.setter.webRoot + '/layuiadmin/style/res/default01.png" style="max-height:100%;margin:auto;display:block;"/>';
                    }
                }}
            ,{field: 'stateName', title: '状态'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 80, align:'center', fixed: 'right',templet:function (record){
                var html = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delFuncModel">移除</a>';
                return html;
            }}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });
	
	table.render({
        elem: '#LAY-table-referrer'
        ,url: layui.setter.webRoot + '/agent/referrer/busi_cfg/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'referrerUserId', title: '推荐人ID'}
            ,{field: 'name', title: '推荐人姓名',templet:function (record){
                return record.referrerUser.name;
             }}
            ,{field: 'phone', title: '推荐人手机号',templet:function (record){
                    return record.referrerUser.phone;
                }}
            ,{field: 'businessTypeName', title: '业务类型'}
			,{field: 'incomeRatio', title: '服务费分成',templet:function (record){
                return record.incomeRatio + '%';
            }}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 80, align:'center', fixed: 'right',templet:function (record){
                var html = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delReferrer">移除</a>';
                return html;
            }}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });
	
    element.on('tab(tab-info)', function (data) {
        if(data.index== 0){
			table.reload('LAY-table')
        }else if(data.index== 1){
            table.reload('LAY-table-type')
		}else if(data.index== 2){
			table.reload('LAY-table-func-model')
		}else if(data.index== 3){
			table.reload('LAY-table-referrer')
	    }
    });

    //事件
    var active = {
        add:function (){
            layer.open({
                type: 2
                ,title: '新增业务'
                ,content: layui.setter.webRoot + '/agent/level/set.html?userId='+userId
                ,maxmin: true
                ,area: ['500px', '400px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/agent/level/set.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.close(index); //关闭弹层
                                    layer.msg('修改成功',{icon: 1},function (){
                                        location.reload();
                                    });
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
		,setLevel:function(){
			layer.open({
                type: 2
                ,title: '修改级别'
                ,content: layui.setter.webRoot + '/agent/cfg_level/update.html?userId='+userId
                ,maxmin: true
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/agent/cfg_level/update.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.close(index); //关闭弹层
                                    layer.msg('修改成功',{icon: 1},function (){
                                        location.reload();
                                    });
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
		}
		,addBusiConfig:function(){
			layer.open({
                type: 2
                ,title: '新增业务配置'
                ,content: layui.setter.webRoot + '/agent/busi_config/add.html?userId='+userId
                ,maxmin: false
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/agent/busi_config/add.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.msg('新增成功',{icon: 1});
                                    table.reload('LAY-table')
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
		}
		,updateBusiConfig:function(obj){
			layer.open({
                type: 2
                ,title: '修改业务配置'
                ,content: layui.setter.webRoot + '/agent/busi_config/update.html?userId='+userId + '&businessType='+obj.data.businessType
                ,maxmin: false
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/agent/busi_config/update.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.msg('新增成功',{icon: 1});
                                    table.reload('LAY-table')
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
		}
		,delBusiConfig:function(obj){
            layer.confirm('确定要移除该业务配置吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/agent/busi_config/del.do',
                    data:{userId:userId,businessType:obj.data.businessType},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
		,addType:function(){
			layer.open({
                type: 2
                ,title: '新增代理类型'
                ,content: layui.setter.webRoot + '/agent/type_rel/add.html?userId='+userId
                ,maxmin: false
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/agent/type_rel/add.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.msg('新增成功',{icon: 1});
                                    table.reload('LAY-table-type')
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
		}
		,delType:function(obj){
            layer.confirm('确定要移除该代理类型吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/agent/type_rel/del.do',
                    data:{userId:userId,typeId:obj.data.typeId},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,addFuncModel:function (){
            layer.open({
                type: 2
                ,title: '新增功能模块'
                ,content: layui.setter.webRoot + '/agent/func_model/add.html?userId='+userId
                ,maxmin: true
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/agent/func_model/add.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.msg('新增成功',{icon: 1});
                                    table.reload('LAY-table-func-model')
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,delFuncModel:function(obj){
            layer.confirm('确定要移除该功能模块吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/agent/func_model/del.do',
                    data:{userId:userId,funcModelId:obj.data.funcModelId},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
		,addreferrer:function(){
			layer.open({
                type: 2
                ,title: '新增推荐人'
                ,content: layui.setter.webRoot + '/agent/referrer/busi_cfg/add.html?userId='+userId
                ,maxmin: true
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/agent/referrer/busi_cfg/add.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.msg('新增成功',{icon: 1});
                                    table.reload('LAY-table-referrer')
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
		}
		,delReferrer:function(obj){
           layer.confirm('确定要移除该业务的推荐人吗？', function(index){
               $.ajax({
                   type: "post",
                   url: layui.setter.webRoot + '/agent/referrer/busi_cfg/del.do',
                   data:{userId:userId,referrerUserId:obj.data.referrerUserId,businessType:obj.data.businessType},
                   dataType:"json",
                   success:function(res){
                       if(res.code == 200){
                           obj.del();
                       }else{
                           layer.msg(res.msg,{icon: 5});
                       }
                   }
               });
               layer.close(index);
           });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
	
	table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
	
	table.on('tool(LAY-table-type)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
		
	table.on('tool(LAY-table-func-model)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
	
	table.on('tool(LAY-table-referrer)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

  exports('agent_info', {})
});